/**
 * 				MochitoClientRMI.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	22.12.2006
 *	 Project:	Mochito-TransferFramework
 *	 Package:	mochitoConnection.interfaces.client.rmi
 *	 Usage:		Mochito RMI Client connection
 * *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochitoConnection.rmi.client;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.LinkedList;

import mochitoConnection.interfaces.client.MochitoClientConnection;
import mochitoConnection.interfaces.server.MochitoServerConnection;
import mochitoConnection.rmi.MochitoConnectionRMI;
import mochitoTransferFactory.MochitoException;
import mochitoTransferFactory.mochitoHost.MochitoHost;
import mochitoTransferableObject.AbstractMochitoTransferableObject;
import mochitoTransferableObject.MochitoTransferableInfo;
import mochitoTransferableObject.MochitoTransferableObject;

/**
 * @author bin04017
 * 
 */
public class MochitoClientRMI extends MochitoConnectionRMI  implements MochitoClientConnection {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6771509643775678089L;

	private MochitoServerConnection mochitoServer = null;

	public MochitoClientRMI(MochitoHost host) throws MochitoException,
			RemoteException {
		super(host);
		init();
	}

	public MochitoClientRMI(String host) throws MochitoException,
			RemoteException {
		super(host);
		init();
	}

	/**
	 * make connection to server
	 * 
	 * @throws Exception
	 */
	protected void init() throws MochitoException {
		if (mochitoServer == null) {
			try {
				System.out.println("rmi://" + this.getHostAsString() + "/Mochito");
				
				mochitoServer = (MochitoServerConnection)Naming
						.lookup("rmi://" + this.getHostAsString() + "/Mochito");
			} catch (Exception e) {
				e.printStackTrace();
				throw new MochitoException(
						"Connection refused. Maybe the server is not running.\n"+e.getMessage());
			}
		}
	}

	public void addMochitoTransferableObject(AbstractMochitoTransferableObject obj) throws RemoteException {
		mochitoServer.addMochitoTransferableObject(obj);
	}

	public AbstractMochitoTransferableObject getMochitoTransferableObject() throws RemoteException {
		return (AbstractMochitoTransferableObject)mochitoServer.getMochitoTransferableObject();
	}

	public long getCount() throws RemoteException {
		return mochitoServer.getCount();
	}

	public LinkedList<MochitoTransferableInfo> getMochitoTransferInfo() throws RemoteException {
		return mochitoServer.getMochitoTransferInfo();
	}

	public MochitoTransferableObject getMochitoTransferableObject(Long id) throws RemoteException {
		return mochitoServer.getMochitoTransferableObject(id);
	}

	public MochitoTransferableObject getMochitoTransferableObject(MochitoTransferableInfo info) throws RemoteException {
		return mochitoServer.getMochitoTransferableObject(info);
	}
}
